package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.CoinType;
import com.coin.service.CoinTypeService;
import com.coin.mapper.CoinTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 李力
 * @description 针对表【coin_type(币种类型)】的数据库操作Service实现
 * @createDate 2025-06-12 10:12:53
 */
@Service
public class CoinTypeServiceImpl extends ServiceImpl<CoinTypeMapper, CoinType>
        implements CoinTypeService {

    /**
     * @Description: 条件分页查询我们的币种类型
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    public Page<CoinType> findByPage(Page<CoinType> page, String code) {
        LambdaQueryWrapper<CoinType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(StrUtil.isNotEmpty(code), CoinType::getCode, code);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 新增货币类型
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCoinType(CoinType coinType) {
        if (coinType == null) {
            throw new RuntimeException("参数为空");
        }
        return save(coinType);
    }

    /**
     * @Description: 修改货币类型
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCoinType(CoinType coinType) {
        if (coinType == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(coinType);
    }

    /**
     * @Description: 修改货币状态
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setStatus(Long id, Byte status) {
        if (id == null || status == null) {
            throw new RuntimeException("参数为空");
        }
        CoinType coinType = getById(id);
        if (coinType == null) {
            throw new RuntimeException("无此币种");
        }
        coinType.setStatus(status);
        return updateById(coinType);
    }

    /**
     * @Description: 查询所有的币种类型
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    public List<CoinType> listByStatus(Byte status) {
        LambdaQueryWrapper<CoinType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(status != null, CoinType::getStatus, status);
        return list(queryWrapper);
    }
}




